package com.treasure.mall.zbqmallapi.controller.withdraw;

import com.treasure.mall.biz.bo.config.WithdrawConfig;
import com.treasure.mall.biz.dto.condition.withdraw.WithdrawSearchCondition;
import com.treasure.mall.biz.dto.withdraw.UserBankInfoDTO;
import com.treasure.mall.biz.dto.withdraw.WithdrawDTO;
import com.treasure.mall.biz.entity.user.UserPO;
import com.treasure.mall.biz.entity.withdraw.UserBankInfoPO;
import com.treasure.mall.biz.mapstruct.withdraw.UserBankInfoMapStruct;
import com.treasure.mall.biz.service.core.BaseConfigService;
import com.treasure.mall.biz.service.core.withdraw.BankService;
import com.treasure.mall.biz.service.core.withdraw.UserBankInfoService;
import com.treasure.mall.biz.service.core.withdraw.WithdrawService;
import com.treasure.mall.biz.vo.withdraw.UserBankInfoVO;
import com.treasure.mall.zbq.common.annotation.CurrentUser;
import com.treasure.mall.zbq.common.base.ApiResult;
import com.treasure.mall.zbq.common.ienum.base.CodeDescEnumHelper;
import com.treasure.mall.zbq.common.ienum.withdraw.WithdrawEnums;
import com.treasure.mall.zbqmallapi.annotation.AccessControl;
import com.treasure.mall.zbqmallapi.vo.withdraw.WithDrawPageInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zoul
 * @version 1.0
 * @date 2020/6/14 16:06
 * @Description
 */
@Slf4j
@RequestMapping("/withdraw")
@RestController
@AccessControl(needLogin = true)
@Api(tags = "提现模块")
public class WithdrawController {
    @Autowired
    private BankService bankService;
    @Autowired
    private UserBankInfoService userBankInfoService;
    @Autowired
    private WithdrawService withdrawService;
    @Autowired
    private BaseConfigService baseConfigService;

    /**
     * 获取所有银行列表
     *
     * @return
     */
    @ApiOperation("获取所有银行列表")
    @GetMapping("/getList")
    @AccessControl
    public ApiResult getList() {
        return ApiResult.ok(bankService.getList());
    }

    /**
     * 添加/修改银行信息
     *
     * @param dto 提交实体
     * @return
     */
    @ApiOperation("添加/修改银行信息")
    @PostMapping("/addBankInfo")
    public ApiResult addBankInfo(@CurrentUser UserPO user, UserBankInfoDTO dto) {
        UserBankInfoPO po = UserBankInfoMapStruct.INSTANCE.dto2po(dto);
        po.setDefaultFlag(false);
        po.setUserPo(user);
        po.setBankPo(bankService.findById(dto.getBankId()));
        return userBankInfoService.addOrUpdateUserBank(po);
    }

    /**
     * 获取用户的银行卡列表
     *
     * @param user 当前用户
     * @return
     */
    @ApiOperation("获取用户的银行卡列表")
    @GetMapping("/getUserBankList")
    public ApiResult getUserBankList(@CurrentUser UserPO user) {
        List<UserBankInfoVO> list = userBankInfoService.getUserBankList(user.getUserId());
        if (list != null) {
            return ApiResult.ok(list);
        }
        return ApiResult.error("还未绑定银行卡");
    }

    /**
     * 设置默认银行卡
     *
     * @param user   用户信息
     * @param infoId 用户银行卡主键Id
     * @return
     */
    @ApiOperation("设置默认银行卡")
    @PostMapping("/setDefault")
    public ApiResult setDefault(@CurrentUser UserPO user
            , @RequestParam(required = false, defaultValue = "0") Long infoId) {
        return userBankInfoService.setUserBankDefault(user.getUserId(), infoId);
    }


    @ApiOperation("删除银行卡")
    @PostMapping("/deleteUserBank")
    public ApiResult deleteUserBank(@CurrentUser UserPO user
            , @RequestParam Long infoId) {
        userBankInfoService.deleteUserBank(user.getUserId(), infoId);
        return ApiResult.ok();
    }


    /**
     * 获取用户默认/选中银行卡信息
     *
     * @param user   当前用户
     * @param infoId 用户银行卡主键Id(0,选择默认)
     * @return
     */
    @ApiOperation("获取用户默认/选中银行卡信息")
    @GetMapping("/getWithdrawBankInfo")
    public ApiResult getWithdrawBankInfo(@CurrentUser UserPO user
            , @RequestParam(required = false, defaultValue = "0") Long infoId) {
        if (infoId == 0L) {
            UserBankInfoVO vo = userBankInfoService.getUserDefaultBank(user.getUserId());
            if (vo != null) {
                return ApiResult.ok(vo);
            }

        } else {
            UserBankInfoPO userBankInfoPO = userBankInfoService.findById(infoId);
            UserBankInfoVO vo = UserBankInfoMapStruct.INSTANCE.po2vo(userBankInfoPO);
            if (vo != null) {
                return ApiResult.ok(vo);
            }
        }
        return ApiResult.error("还未绑定银行卡");
    }

    /**
     * 提现申请页数据
     *
     * @param user   用户Id
     * @param infoId 用户银行卡主键Id(0,选择默认)
     * @return
     */
    @ApiOperation("提现申请页数据")
    @GetMapping("/withdrawPage")
    public ApiResult withdrawPage(@CurrentUser UserPO user
            , @RequestParam(required = false, defaultValue = "0") Long infoId) {
        WithdrawConfig withdrawConfig = baseConfigService.getConfig().getWithdrawConfig();
        WithDrawPageInfoVo vo = new WithDrawPageInfoVo();
        vo.setConfigVo(withdrawConfig);
        vo.setUserPo(user);
        if (infoId == 0L) {
            UserBankInfoVO userBankInfoVO = userBankInfoService.getUserDefaultBank(user.getUserId());
            vo.setBankInfoVO(userBankInfoVO);
        } else {
            UserBankInfoPO userBankInfoPO = userBankInfoService.findById(infoId);
            UserBankInfoVO userBankInfoVO = UserBankInfoMapStruct.INSTANCE.po2vo(userBankInfoPO);
            vo.setBankInfoVO(userBankInfoVO);
        }
        return ApiResult.ok(vo);
    }


    /**
     * 申请提现
     *
     * @param user 当前用户
     * @param dto  提现申请实体
     * @return
     */
    @ApiOperation("申请提现")
    @PostMapping("/apply")
    public ApiResult applyWithdraw(@CurrentUser UserPO user, WithdrawDTO dto, int type) {

        WithdrawEnums.WithdrawType withdrawType = CodeDescEnumHelper.getEnumTypeByCode(WithdrawEnums.WithdrawType.class, type);
        if (withdrawType != null) {
            dto.setWithdrawType(withdrawType);
        } else {
            return ApiResult.error("提现失败");
        }
        return withdrawService.applyWithdraw(dto, user);
    }

    /**
     * 获取我的提现申请列表
     *
     * @param user      用户信息
     * @param condition 查询条件
     * @param pageIndex 页码
     * @param pageSize  每页显示
     * @return
     */
    @ApiOperation("获取我的提现申请列表")
    @GetMapping("/applyList")
    public ApiResult applyList(@CurrentUser UserPO user, WithdrawSearchCondition condition,
                               @RequestParam(required = false, defaultValue = "1") int pageIndex,
                               @RequestParam(required = false, defaultValue = "20") Integer pageSize) {
        condition.setUserNo(user.getMobile());
        return ApiResult.ok(withdrawService.findAll(pageIndex, pageSize, condition));
    }

    /**
     * 取消提现申请
     *
     * @param user    用户信息
     * @param applyId 提现申请主键ID
     * @return
     */
    @ApiOperation("取消提现申请")
    @PostMapping("/cancelApply")
    public ApiResult cancelApply(@CurrentUser UserPO user
            , @RequestParam(required = false, defaultValue = "0") Long applyId) {
        return withdrawService.cancelWithdrawStatus(user.getUserId(), applyId);
    }
}
